package com.anlogic.sdk.bootimage.internal.ui.common;

import com.anlogic.sdk.bootimage.internal.ui.composite.AbsBootgenComposite;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class BootgenCheckBoxField implements IExtBootgenField<Button, Boolean> {
	private Button checkBox;

	private AbsBootgenComposite bootgenComposite;

	private IModifyListener<Boolean> modifyListener;

	public BootgenCheckBoxField(AbsBootgenComposite bootgenComposite) {
		this.bootgenComposite = bootgenComposite;
	}

	public void createField(Composite container, String labelText) {
		this.checkBox = new Button(container, 32);
		this.checkBox.setText(labelText);
		this.checkBox.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!BootgenCheckBoxField.this.bootgenComposite.isSkipNotifactions()
						&& BootgenCheckBoxField.this.modifyListener != null)
					BootgenCheckBoxField.this.modifyListener.valueChanged(BootgenCheckBoxField.this.getSelection());
			}
		});
	}

	public void updateSelection(boolean selection) {
		this.checkBox.setSelection(selection);
	}

	public void setDefaultGridData(int hSpan) {
		GridDataFactory.swtDefaults().span(hSpan, 1).applyTo((Control) getControl());
	}

	public Boolean getSelection() {
		return Boolean.valueOf(this.checkBox.getSelection());
	}

	public Button getControl() {
		return this.checkBox;
	}

	public void setSelection(Boolean selection) {
		this.checkBox.setSelection(selection.booleanValue());
	}

	public void setModifyListener(IModifyListener<Boolean> modifyListener) {
		this.modifyListener = modifyListener;
	}

	public void setEnabled(boolean enabled) {
		getControl().setEnabled(enabled);
	}
}
